package com.jlcindia.action.classes;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jlc.connPage.JDBCUtil;

public class RegisterAction {
	public RegisterAction() {
		System.out.println("RegisterAction() Cons Invoked, means object is created");
	}
	Connection con=null;
	PreparedStatement ps=null;
	ResultSet rs=null;

	public String registerUser(HttpServletRequest request,
			HttpServletResponse response) {
		System.out.println("~~~~~~registerUser() method invoked~~~~~");
		String page = "register.jsp";

		String name = request.getParameter("name");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		long ph=Long.parseLong(phone);
		String password = request.getParameter("password");
		String cpassword = request.getParameter("cpassword");
		
		try {
			con=JDBCUtil.getMysqlConnection();
			ps=con.prepareStatement("insert into userInfo values('"+name+"','"+email+"',"+ph+",'"+password+"','"+cpassword+"')");
			int a=ps.executeUpdate();
			if(a>0){
				page = "register.jsp";
				request.setAttribute("SUCCESS", "Registered Successfully");
			}else{
				page = "register.jsp";
				request.setAttribute("ERROR", "Register failed");
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JDBCUtil.cleanup(con, ps, rs);
		}

		return page;

	}

}
